﻿
//Author: 张亚涛
//createtime:2015-12-23 22:46:34
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Park" 表的方法;
    /// </summary>
    public class ParkBase
    {
        const string SQL_Select = "select [ParkID],[ParkName],[ParentID],[ParentPath],[Depth],[CommuntyID] from [Park] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Park表的所有数据  
        /// </summary>
        ///  <param name="CommuntyID">小区ID</param>
        ///  <param name="ParentID">父ID</param>
        /// <returns></returns>        
        public static List<Park> GetAllPark(string CommuntyID, string ParentID)
        {
            List<Park> parkList = new List<Park>();
            SqlDataReader reader = null;

            string sql = SQL_Select + " where CommuntyID='" + CommuntyID + "' and ParentID='" + ParentID + "'";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Park park = new Park(reader);
                parkList.Add(park);
            }
            reader.Close();
            return parkList;
        }
        /// <summary>
        /// 根据Park表的ParkID字段返回数据 
        /// </summary>
        /// <param name="parkID">ParkID</param>
        /// <returns></returns>       
        public static Park GetParkByParkID(long parkID)
        {
            Park park = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ParkID] = @ParkID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@ParkID",parkID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                park = new Park(reader);
            }
            reader.Close();
            return park;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Park表的ParkID字段删除数据 
        /// </summary>
        /// <param name="parkID">parkID</param>
        /// <returns></returns>        
        public static int DeleteParkByParkID(long parkID)
        {
            string sql = "delete from [Park] where [ParkID] = @ParkID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@ParkID",parkID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Park park)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@ParkID",park.ParkID),
            new SqlParameter("@ParkName",park.ParkName),
            new SqlParameter("@ParentID",park.ParentID),
            new SqlParameter("@ParentPath",park.ParentPath),
            new SqlParameter("@Depth",park.Depth),
            new SqlParameter("@CommuntyID",park.CommuntyID)    
            };
            return paras;
        }
        /// <summary>
        /// 根据Park表的ParkID字段更新数据 
        /// </summary> 
        /// <param name="Park">park</param>
        /// <returns></returns>       
        public static int UpdateParkByParkID(Park park)
        {
            string sql = "update [Park] set [ParkName] = @ParkName,[ParentID] = @ParentID,[ParentPath] = @ParentPath,[Depth] = @Depth,[CommuntyID] = @CommuntyID where [ParkID] = @ParkID";
            SqlParameter[] paras = PrepareCommandParameters(park);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Park表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="park">Park</param>       
        /// <returns></returns>        
        public static int AddPark(Park park)
        {
            string sql = "insert into [Park]([ParkName],[ParentID],[ParentPath],[Depth],[CommuntyID])values(@ParkName,@ParentID,@ParentPath,@Depth,@CommuntyID)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(park);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回Park表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllPark_SQL(string CommuntyID, string ParentID)
        {
            string sql = SQL_Select + " where CommuntyID='" + CommuntyID + "' and ParentID='" + ParentID + "'";
            return sql;
        }
        #endregion
    }
}